package csc.daonjpa.java.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;
import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import csc.daonjpa.java.domain.Account;


@Repository
public class AccountDAO {
	
	// Injected database connection:
    @PersistenceContext private EntityManager em;
    
    @Transactional
    public void persist(Account account) {
        em.persist(account);
    }

    @Transactional
	public List<Account> getAccounts() {
		TypedQuery<Account> query = em.createQuery("SELECT a FROM Account a",
				Account.class);
		List<Account> accounts = null;
		accounts = query.getResultList();
		return accounts;
	}
    @Transactional
	public boolean addAccount(Account account) {
		 em.persist(account);
		return true;
	}
    @Transactional
	public List<Account> getAccountsByCustomerId(String custLoginId) {
		TypedQuery<Account> query = null;
		try {
			query = em.createQuery("SELECT a FROM Account a WHERE custId = ?",
					Account.class).setParameter(1, custLoginId);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return query.getResultList();
	}
}
